﻿using DevExpress.XtraEditors;
using LSQL;
using Shopfloormanagement;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Shopfloormanagement
{
    public partial class Form_LPA_Audit : DevExpress.XtraEditors.XtraForm
    {
        public string FormType = "Add";
        public int ID = -1;
        public Form_LPA_Audit()
        {
            InitializeComponent();
        }
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (txt_Scope.Text != "")
            {
                if (FormType == "Add")
                {
                    if (SQL.ReturnRowsCount3(SystemInfo.SQL, "LPA_AuditScope", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Scope='" + txt_Scope.Text + "' and Del_tag=0") == 0)
                    {
                        if (SQL.Insert(SystemInfo.SQL, "LPA_AuditScope", "AuditFactortyID,AuditDepartmentID,Scope", $"{SystemInfo.readFactory},{SystemInfo.readDepartment},'{txt_Scope.Text}'") > 0)
                        {
                            txt_Scope.Text = "";
                            Form_LPA.frm.LoadAuditScope();
                            txt_Scope.Focus();
                        }
                    }
                    else
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("The content is repetitive!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else if (FormType == "Revise")
                {
                    if (ID != -1)
                    {
                        string AuditFactortyID = "AuditFactortyID=" + SystemInfo.readFactory;
                        string AuditDepartmentID = "AuditDepartmentID=" + SystemInfo.readDepartment;
                        string Scope = "Scope='" + txt_Scope.Text + "'";
                        if (SQL.Update(SystemInfo.SQL, "LPA_AuditScope", $"{AuditFactortyID},{AuditDepartmentID},{Scope}", "ID=" + ID) == true)
                        {
                            this.Close();
                            Form_LPA.frm.LoadAuditScope();
                        }
                        else
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("Delete failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Please enter audit scope!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txt_Scope.Focus();
            }
        }
        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            if (ID != -1)
            {
                if (SQL.Update(SystemInfo.SQL, "LPA_AuditScope", "Del_tag=1", "ID=" + ID) == true)
                {
                    this.Close();
                    Form_LPA.frm.LoadAuditScope();
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("Delete failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void btn_Cancel2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}